﻿// ***********************************************************************
// Assembly         : DonNicky.Common.NET4
// Author           : donnicky
// Created          : 11-28-2012
//
// Last Modified By : donnicky
// Last Modified On : 11-28-2012
// ***********************************************************************
using System.ComponentModel.DataAnnotations;
using System.IO;

namespace DonNicky.Common.Validation
{
    /// <summary>
    /// Checks if the directory of the specified file exists
    /// </summary>
    public class FileDirExistsAttribute : ValidationAttribute
    {
        /// <summary>
        /// Determines whether the directory of the specified file exists.
        /// </summary>
        /// <param name="value">The value of the object to validate.</param>
        /// <returns>true if the specified directory path exists (or provided value is null or empty); otherwise, false.</returns>
        public override bool IsValid(object value)
        {
            if (value == null || string.IsNullOrEmpty(value.ToString()))
                return true;
            var dirInfo = new FileInfo(value.ToString()).Directory;
            return dirInfo != null && dirInfo.Exists || Directory.Exists(value.ToString());
        }
    }
}
